/**
 * Title: jFamilyTree File System provider
 * Description: jFamilyTree File System provider
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright: (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.provider.fs;

import java.awt.Color;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import jFamilyTree.model.IEdge;
import jFamilyTree.model.Tree;
import jFamilyTree.model.IProvider;
import jFamilyTree.model.IProviderContext;
import jFamilyTree.model.Model;
import jFamilyTree.model.Settings;
import jFamilyTree.provider.fs.FsNode;

/**
 * Provider for file system
 * 
 * @author Bernard Bou
 */
public class Provider implements IProvider
{
	static private Color theBackgroundColor = Color.WHITE;

	/**
	 * Provider context
	 */
	private IProviderContext theContext;

	/**
	 * Constructor
	 */
	public Provider()
	{
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.model.IProvider#set(jFamilyTree.model.IProviderContext)
	 */
	public void connect(IProviderContext thisContext)
	{
		theContext = thisContext;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.model.IProvider#makeModel(java.lang.String)
	 */
	public Model makeModel(String thisSource)
	{
		Tree thisTree = makeTree(thisSource, false);
		if (thisTree == null)
			return null;

		// settings
		boolean asTree = thisTree.getRoot().getChildren().size() < 6;
		Settings theseSettings = new Settings();
		theseSettings.theBackColor = theBackgroundColor;
		theseSettings.theNodeBackColor = FsNode.theDefaultFileBackColor;
		theseSettings.theNodeForeColor = FsNode.theDefaultFileForeColor;

		theseSettings.theOrientation = asTree ? "south" : "radial";
		theseSettings.theHasToolbarFlag = true;
		theseSettings.theBackColor = new Color(0xf5f5f0);
		theseSettings.theFontFace = "Dialog";
		theseSettings.theFontSize = 20;
		theseSettings.theFontSizeStep = 2;
		theseSettings.theExpansion = .9;
		theseSettings.theSweep = 1.2;
		theseSettings.theHasToolbarFlag = true;
		theseSettings.theHasStatusbarFlag = true;
		theseSettings.theFocusOnHoverFlag = true;
		theseSettings.theTreeEdgeColor = Color.GRAY;
		theseSettings.theTreeEdgeStyle = IEdge.DASH | IEdge.FROMCIRCLE | IEdge.FROMSOLID;
		return new Model(thisTree, theseSettings);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.model.IProvider#makeTree(java.lang.String, boolean)
	 */
	public Tree makeTree(String thisSource, boolean checkRecursion)
	{
		String thisRootPath = thisSource;
		if (thisRootPath == null)
			thisRootPath = theContext.getParameter("fsroot");
		if (thisRootPath == null)
			return null;

		// try url syntax
		try
		{
			URL thisURL = new URL(thisRootPath);
			String thisProtocol = thisURL.getProtocol();
			if (thisProtocol.equals("file"))
				thisRootPath = thisURL.getFile();
		}
		catch (MalformedURLException e)
		{
		}

		// root
		File thisRoot = new File(thisRootPath);
		if (!thisRoot.exists())
		{
			System.err.println("Fs: Can't get file root : " + thisRoot.getName() + " does not exist.");
			return null;
		}
		if (!thisRoot.isDirectory())
		{
			System.err.println("Fs: Can't get folder : " + thisRoot.getName() + " is not a directory.");
			return null;
		}

		// graph
		FsNode thisRootNode = new FsNode(null, thisRoot);
		return new Tree(thisRootNode, null);
	}
}
